package org.Lin.controller;

import org.Lin.service.UserService;
import org.json.JSONObject;

import java.io.*;
import java.net.Socket;

public class RegisterController extends BaseController {

    private final UserService userService = new UserService();

    public void handleRegister(JSONObject request, Socket socket) throws IOException {
        int userID = request.getInt("userID");
        String name = request.getString("name");
        String password = request.getString("password");
        String email = request.getString("email");

        boolean success = userService.registerUser(userID, name, password, email);
        if (success) {
            JSONObject response = createSuccessResponse("注册成功");
            sendResponse(socket, response);
        } else {
            JSONObject response = createErrorResponse("注册失败：用户已存在");
            sendResponse(socket, response);
        }
    }

    private void sendResponse(Socket socket, JSONObject response) throws IOException {
        PrintWriter out = new PrintWriter(socket.getOutputStream(), true);
        out.println(response.toString());
    }
}
